<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <div id="app">
    <h1>todo list</h1>
    <input type="text" v-on:change="add" v-model="msg" placeholder="请输入待办事项">
    <button @click="add">添加</button>
    <p>待办事项</p>
    <ul>
      <li v-for="(item, index) in todoThings" :key="index">
        <span>{{item}}</span>
        <button @click="del(index)">删除</button>
      </li>
    </ul>
    <div v-show="doneThings.length">
      <p>完成事项</p>
      <ul>
        <li v-for="(item, index) in doneThings" :key="index">
          <span>{{item}}</span>
        </li>
      </ul>
    </div>
    
  </div>
  <script src="./vue.js"></script>
  <script>
    let vm = new Vue({
      el: '#app',
      data: {
        // 具体事项
        msg: '',
        // 待完成的事项
        todoThings: [],
        // 已完成的事项
        doneThings: []
      },
      created() {
        this.todoThings = JSON.parse(localStorage.getItem('todoThings'));
      },
      methods: {
        // 添加待办事项
        add() {
          if(this.msg != '') {
            this.todoThings.push(this.msg);
            localStorage.setItem('todoThings', JSON.stringify(this.todoThings));
            // 清空输入框
            this.msg = '';
          }
        },
        // 完成事项
        del(index) {
          // 添加到已完成事项
          this.doneThings.push(this.todoThings[index]);
            localStorage.setItem('doneThings', JSON.stringify(this.doneThings));
          // 从待完成删除
          this.todoThings.splice(index, 1);
            localStorage.setItem('todoThings', JSON.stringify(this.todoThings));
        }
      },
    })
  </script>
</body>
</html>